﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using QSAspMvc.Models;
using QSAspMvc.Data;
using ValidationFramework;

namespace QSAspMvc
{
	// Note: For instructions on enabling IIS6 or IIS7 classic mode, 
	// visit http://go.microsoft.com/?LinkId=9394801

	public class MvcApplication : System.Web.HttpApplication
	{
		public static void RegisterRoutes(RouteCollection routes)
		{
			routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

			routes.MapRoute(
				"Default",                                              // Route name
				"{controller}/{action}/{id}",                           // URL with parameters
				new { controller = "Product", action = "Browse", id = "" }  // Parameter defaults
			);
		}

		protected void Application_Start()
		{
			ValidationBootStrapper.Boot();
			RegisterRoutes(RouteTable.Routes);
		}

		// Application Events
		protected void Session_Start(object sender, EventArgs e)
		{
			InitializeRepositories();
		}

		// Repository
		private static void InitializeRepositories()
		{
			InitProductRepository();
		}
		private static void InitProductRepository()
		{
			var repo = new SessionProductRepository();
			for (var i = 0; i < 10; i++)
			{
				var id = i + 1; // convert to 1 index
				var product = new Product()
				{
					ProductId = id,
					Discontinued = false,
					ProductName = string.Format("Product{0}", id),
					UnitPrice = id * 10,
					ReorderLevel = id * 100
				};
				repo.SaveProduct(product);
			}
		}
	}
}